#include <iostream>
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include "SDL/SDL_mixer.h"
#include <string>

using namespace std;

//Screen attributes
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

//The surfaces
SDL_Surface *background = NULL;
SDL_Surface *message = NULL;
SDL_Surface *mainscreen = NULL;
SDL_Surface *blackButtonMessage = NULL;
SDL_Surface *redButtonMessage = NULL;
SDL_Surface *greenButtonMessage = NULL;
SDL_Surface *yellowButtonMessage = NULL;
SDL_Surface *orangeButtonMessage = NULL;
SDL_Surface *whiteButtonMessage = NULL;
SDL_Surface *creditsMessage = NULL;


//The event structure
SDL_Event event;

//The font
TTF_Font *font = NULL;
TTF_Font *smallfont = NULL;

//The color of the font
SDL_Color textColor = { 0, 0, 0 };

SDL_Surface *load_image (std::string filename)
{
  //The image that's loaded
  SDL_Surface *loadedImage = NULL;

  //The optimized surface that will be used
  SDL_Surface *optimizedImage = NULL;

  //Load the image
  loadedImage = IMG_Load (filename.c_str ());

  //If the image loaded
  if (loadedImage != NULL)
  {
	//Create an optimized surface
	optimizedImage = SDL_DisplayFormat (loadedImage);

	//Free the old surface
	SDL_FreeSurface (loadedImage);

	//If the surface was optimized
	if (optimizedImage != NULL)
	{
	  //Color key surface
	  SDL_SetColorKey (optimizedImage, SDL_SRCCOLORKEY,
					   SDL_MapRGB (optimizedImage->format, 0, 0xFF, 0xFF));
	}
  }

  //Return the optimized surface
  return optimizedImage;
}

void apply_surface (int x, int y, SDL_Surface * source, SDL_Surface * destination, SDL_Rect * clip = NULL)
{
  //Holds offsets
  SDL_Rect offset;

  //Get offsets
  offset.x = x;
  offset.y = y;

  //Blit
  SDL_BlitSurface (source, clip, destination, &offset);
}

bool init ()
{
  //Initialize all SDL subsystems
  if (SDL_Init (SDL_INIT_EVERYTHING) == -1)
  {
	return false;
  }

  //Set up the screen
  mainscreen =
	SDL_SetVideoMode (SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);

  //If there was an error in setting up the screen
  if (mainscreen == NULL)
  {
	return false;
  }

  //Initialize SDL_ttf
  if (TTF_Init () == -1)
  {
	return false;
  }

  //Set the window caption
  SDL_WM_SetCaption ("Project Interface", NULL);

  //If everything initialized fine
  return true;
}

bool load_files ()
{

/*
//***********Loading Background Image**************
    //Load the background image
    background = load_image( "background.png" );


    //If there was a problem in loading the background
    if( background == NULL )
    {
        return false;
    }

//*************End Loading Background Image**************
*/


//***********Loading Fonts*************
  //Open the font
  font = TTF_OpenFont ("AgentOrange.ttf", 20);
  smallfont = TTF_OpenFont ("AgentOrange.ttf", 13);

  //If there was an error in loading the font
  if (font == NULL)
  {
	return false;
  }
//***********End Loading Fonts***************



  //If everything loaded fine
  return true;
}


void clean_up ()
{
  //Free the surfaces
  SDL_FreeSurface (background);


  //Close the font
  TTF_CloseFont (font);

  //Quit SDL_ttf
  TTF_Quit ();

  //Quit SDL
  SDL_Quit ();
}

//***********main program*************

int main (int argc, char *args[])
{
  //Quit flag
  bool quit = false;

  //Initialize
  if (init () == false)
  {
	return 1;
  }

  //Load the files
  if (load_files () == false)
  {
	return 1;
  }


  int whichscreen = 1;

  //While the user hasn't quit
  while (quit == false)
  {
	if (whichscreen == 1)
	{
	  //$$$$$$$$$$ BEGIN STEPS TO MAKE SCREEN 1 $$$$$$$$$$

	  Uint32 mainscreenColor;	//needed for changing color of background
	  SDL_Rect mainscreenRect;
	  mainscreenRect.x = mainscreenRect.y = 0;
	  mainscreenRect.w = mainscreen->w;
	  mainscreenRect.h = mainscreen->h;
	  mainscreenColor = SDL_MapRGB (mainscreen->format, 20, 96, 255);
	  SDL_FillRect (mainscreen, &mainscreenRect, mainscreenColor);

	  SDL_Rect b1;	//Making a red button (B1)
	  Uint32 b1color;
	  b1.x = 50;
	  b1.y = 100;
	  b1.w = 100;
	  b1.h = 50;
	  b1color = SDL_MapRGB (mainscreen->format, 255, 0, 0);
	  SDL_FillRect (mainscreen, &b1, b1color);
	  
	  SDL_Rect b2;	//Making a yellow button (B2)
	  Uint32 b2color;
	  b2.x = 50;
	  b2.y = 200;
	  b2.w = 100;
	  b2.h = 50;
	  b2color = SDL_MapRGB (mainscreen->format, 255, 255, 0);
	  SDL_FillRect (mainscreen, &b2, b2color);	  

	  SDL_Rect b3;	//Making a white button (B3)
	  Uint32 b3color;
	  b3.x = 50;
	  b3.y = 300;
	  b3.w = 100;
	  b3.h = 50;
	  b3color = SDL_MapRGB (mainscreen->format, 255, 255, 255);
	  SDL_FillRect (mainscreen, &b3, b3color);


	  //*******adding a message to screen*********
	  //Render the text
	  message = TTF_RenderText_Solid (font, "Welcome to the Project!", textColor);
	  redButtonMessage = TTF_RenderText_Solid (font, "Go to the second page", textColor);
	  yellowButtonMessage = TTF_RenderText_Solid (font, "Go to the third page", textColor);
	  whiteButtonMessage = TTF_RenderText_Solid (font, "Go to the credits page", textColor);

	  //If there was an error in rendering the text
	  if (message == NULL)
	  {
		return 1;
	  }
	  if (redButtonMessage == NULL)
	  {
		return 1;
	  }
	  if (yellowButtonMessage == NULL)
	  {
		return 1;
	  }
	  if (whiteButtonMessage == NULL)
	  {
		return 1;
	  }
	

	  //Show the message on the screen
	  apply_surface ((SCREEN_WIDTH - message->w) / 2, 18, message,
					 mainscreen);
	  apply_surface (175,112.5,redButtonMessage,mainscreen); 
	  apply_surface (175,212.5,yellowButtonMessage, mainscreen);
	  apply_surface (175,312.5,whiteButtonMessage, mainscreen);

	  //Free the message
	  SDL_FreeSurface (message);
	  SDL_FreeSurface (redButtonMessage);
	  SDL_FreeSurface (yellowButtonMessage);
	  SDL_FreeSurface (whiteButtonMessage);
	  //*******end adding a message to screen*********

	  if (SDL_Flip (mainscreen) == -1)
 	  {
	    return 1;
	  }
	  //$$$$$$$$ END STEPS TO MAKE SCREEN 1 $$$$$$$$$$

	  
	  //$$$$$$$$ HANDLING EVENTS OF SCREEN 1 $$$$$$$$$

	  //While there's events to handle
	  while (SDL_PollEvent (&event))
	  {
		//The mouse coordinates
		int x = 0, y = 0;

		//If a mouse button was pressed
		if (event.type == SDL_MOUSEBUTTONDOWN)
		{

		  //If the left mouse button was pressed
		  if (event.button.button == SDL_BUTTON_LEFT)
		  {
			//Get the mouse offsets
			x = event.button.x;
			y = event.button.y;

			//If the mouse is over the red button
			if ((x > b1.x) && (x < b1.x + b1.w) && (y > b1.y)
				&& (y < b1.y + b1.h))
			{
			  cout << "The red button has been clicked" << endl;
			  cout << "whichscreen is now 2" << endl;
			  whichscreen = 2;

			}

			//If the mouse is over the yellow button
			if ((x > b2.x) && (x < b2.x + b2.w) && (y > b2.y)
				&& (y < b2.y + b2.h))
			{
			  cout << "The yellow button has been clicked" << endl;
			  cout << "whichscreen is now 3" << endl;
			  whichscreen = 3;
			}

			//If the mouse is over the white button
			if ((x > b3.x) && (x < b3.x + b3.w) && (y > b3.y)
				&& (y < b3.y + b3.h))
			{
			  cout << "The white button has been clicked" << endl;
			  cout << "whichscreen is now 4" << endl;
			  whichscreen = 4;
			}
		  }
		}
	    //If the user has Xed out the window
	    if (event.type == SDL_QUIT)
	    {
		//Quit the program
		quit = true;
	    }


	  }

	  //$$$$$$$$ END HANDLING EVENTS OF SCREEN 1 $$$$$$$$$

	}

	if (whichscreen == 2)
	{

	  //$$$$$$$$$$ BEGIN STEPS TO MAKE SCREEN 2 $$$$$$$$$$

	  Uint32 mainscreenColor;	//needed for changing color of background
	  SDL_Rect mainscreenRect;
	  mainscreenRect.x = mainscreenRect.y = 0;
	  mainscreenRect.w = mainscreen->w;
	  mainscreenRect.h = mainscreen->h;
	  mainscreenColor = SDL_MapRGB (mainscreen->format, 255, 0, 0);
	  SDL_FillRect (mainscreen, &mainscreenRect, mainscreenColor);

	  SDL_Rect b4;	//making a black button (B4)
	  Uint32 b4color;
	  b4.x = 50;
	  b4.y = 100;
	  b4.w = 100;
	  b4.h = 50;
	  b4color = SDL_MapRGB (mainscreen->format, 0, 0, 0);
	  SDL_FillRect (mainscreen, &b4, b4color);

	  //*******adding a message to screen*********
	  //Render the text
	  message = TTF_RenderText_Solid (font, "Welcome to Page 2!", textColor);
	  blackButtonMessage = TTF_RenderText_Solid (font, "Go back to the home page", textColor);

	  //If there was an error in rendering the text
	  if (message == NULL)
	  {
		return 1;
	  }
	  if (blackButtonMessage == NULL)
	  {
		return 1;
	  }

	  //Show the message on the screen
	  apply_surface ((SCREEN_WIDTH - message->w) / 2, 18, message,
					 mainscreen);
	  apply_surface (175,112.5,blackButtonMessage,mainscreen); 

	  //Free the message
	  SDL_FreeSurface (message);
	  SDL_FreeSurface (blackButtonMessage);
	  //*******end adding a message to screen*********

	  if (SDL_Flip (mainscreen) == -1)
 	  {
	    return 1;
	  }
	  //$$$$$$$$ END STEPS TO MAKE SCREEN 2 $$$$$$$$$$
	
	  //$$$$$$$$ HANDLING EVENTS OF SCREEN 2 $$$$$$$$$

	  //While there's events to handle
	  while (SDL_PollEvent (&event))
	  {
		//The mouse coordinates
		int x = 0, y = 0;

		//If a mouse button was pressed
		if (event.type == SDL_MOUSEBUTTONDOWN)
		{

		  //If the left mouse button was pressed
		  if (event.button.button == SDL_BUTTON_LEFT)
		  {
			//Get the mouse offsets
			x = event.button.x;
			y = event.button.y;

			//If the mouse is over the red button
			if ((x > b4.x) && (x < b4.x + b4.w) && (y > b4.y)
				&& (y < b4.y + b4.h))
			{
			  cout << "The black button has been clicked" << endl;
			  cout << "whichscreen is now 1" << endl;
			  whichscreen = 1;
			}
		  }
		}
	    //If the user has Xed out the window
	    if (event.type == SDL_QUIT)
	    {
		//Quit the program
		quit = true;
	    }
	  }
	  //$$$$$$$$ END HANDLING EVENTS OF SCREEN 2 $$$$$$$$$
	}




	if (whichscreen == 3)
	{

	  //$$$$$$$$$$ BEGIN STEPS TO MAKE SCREEN 3 $$$$$$$$$$

	  Uint32 mainscreenColor;	//needed for changing color of background
	  SDL_Rect mainscreenRect;
	  mainscreenRect.x = mainscreenRect.y = 0;
	  mainscreenRect.w = mainscreen->w;
	  mainscreenRect.h = mainscreen->h;
	  mainscreenColor = SDL_MapRGB (mainscreen->format, 255,255, 0);
	  SDL_FillRect (mainscreen, &mainscreenRect, mainscreenColor);

	  SDL_Rect b5;	//making a green button (B5)
	  Uint32 b5color;
	  b5.x = 50;
	  b5.y = 200;
	  b5.w = 100;
	  b5.h = 50;
	  b5color = SDL_MapRGB (mainscreen->format, 0, 153, 76);
	  SDL_FillRect (mainscreen, &b5, b5color);

	  //*******adding a message to screen*********
	  //Render the text
	  message = TTF_RenderText_Solid (font, "Welcome to the Third Page!", textColor);
	  greenButtonMessage = TTF_RenderText_Solid (font, "Go back to the home page", textColor);

	  //If there was an error in rendering the text
	  if (message == NULL)
	  {
		return 1;
	  }
	  if (greenButtonMessage == NULL)
	  {
		return 1;
	  }

	  //Show the message on the screen
	  apply_surface ((SCREEN_WIDTH - message->w) / 2, 18, message,
					 mainscreen);
	  apply_surface (175,212.5,greenButtonMessage,mainscreen); 

	  //Free the message
	  SDL_FreeSurface (message);
	  SDL_FreeSurface (greenButtonMessage);
	  //*******end adding a message to screen*********

	  if (SDL_Flip (mainscreen) == -1)
 	  {
	    return 1;
	  }
	  //$$$$$$$$ END STEPS TO MAKE SCREEN 3 $$$$$$$$$$
	
	  //$$$$$$$$ HANDLING EVENTS OF SCREEN 3 $$$$$$$$$

	  //While there's events to handle
	  while (SDL_PollEvent (&event))
	  {
		//The mouse coordinates
		int x = 0, y = 0;

		//If a mouse button was pressed
		if (event.type == SDL_MOUSEBUTTONDOWN)
		{

		  //If the left mouse button was pressed
		  if (event.button.button == SDL_BUTTON_LEFT)
		  {
			//Get the mouse offsets
			x = event.button.x;
			y = event.button.y;

			//If the mouse is over the green button
			if ((x > b5.x) && (x < b5.x + b5.w) && (y > b5.y)
				&& (y < b5.y + b5.h))
			{
			  cout << "The green button has been clicked" << endl;
			  cout << "whichscreen is now 1" << endl;
			  whichscreen = 1;
			}
		  }
		}
	    //If the user has Xed out the window
	    if (event.type == SDL_QUIT)
	    {
		//Quit the program
		quit = true;
	    }
	  }
	  //$$$$$$$$ END HANDLING EVENTS OF SCREEN 3 $$$$$$$$$
	}




	if (whichscreen == 4)
	{

	  //$$$$$$$$$$ BEGIN STEPS TO MAKE CREDITS PAGE $$$$$$$$$$

	  Uint32 mainscreenColor;	//needed for changing color of background
	  SDL_Rect mainscreenRect;
	  mainscreenRect.x = mainscreenRect.y = 0;
	  mainscreenRect.w = mainscreen->w;
	  mainscreenRect.h = mainscreen->h;
	  mainscreenColor = SDL_MapRGB (mainscreen->format, 255, 255, 255);
	  SDL_FillRect (mainscreen, &mainscreenRect, mainscreenColor);

	  SDL_Rect b6;	//making an orange button (B6)
	  Uint32 b6color;
	  b6.x = 50;
	  b6.y = 300;
	  b6.w = 100;
	  b6.h = 50;
	  b6color = SDL_MapRGB (mainscreen->format, 255, 128, 0);
	  SDL_FillRect (mainscreen, &b6, b6color);

	  //*******adding a message to screen*********
	  //Render the text
	  message = TTF_RenderText_Solid (font, "Welcome to the Credits Page!", textColor);
	  orangeButtonMessage = TTF_RenderText_Solid (font, "Go back to the home page", textColor);
	  creditsMessage = TTF_RenderText_Solid (smallfont, "This project has been brought to you by Adam Zebrowski", textColor);

	  //If there was an error in rendering the text
	  if (message == NULL)
	  {
		return 1;
	  }
	  if (orangeButtonMessage == NULL)
	  {
		return 1;
	  }
	  if (creditsMessage == NULL)
	  {
		return 1;
	  }

	  //Show the message on the screen
	  apply_surface ((SCREEN_WIDTH - message->w) / 2, 18, message,
					 mainscreen);
	  apply_surface (175,312.5,orangeButtonMessage,mainscreen); 
	  apply_surface ((SCREEN_WIDTH - creditsMessage->w)/2,200,creditsMessage,mainscreen);

	  //Free the message
	  SDL_FreeSurface (message);
	  SDL_FreeSurface (orangeButtonMessage);
	  SDL_FreeSurface (creditsMessage);
	  //*******end adding a message to screen*********

	  if (SDL_Flip (mainscreen) == -1)
 	  {
	    return 1;
	  }
	  //$$$$$$$$ END STEPS TO MAKE CREDITS PAGE $$$$$$$$$$
	
	  //$$$$$$$$ HANDLING EVENTS OF CREDITS PAGE $$$$$$$$$

	  //While there's events to handle
	  while (SDL_PollEvent (&event))
	  {
		//The mouse coordinates
		int x = 0, y = 0;

		//If a mouse button was pressed
		if (event.type == SDL_MOUSEBUTTONDOWN)
		{

		  //If the left mouse button was pressed
		  if (event.button.button == SDL_BUTTON_LEFT)
		  {
			//Get the mouse offsets
			x = event.button.x;
			y = event.button.y;

			//If the mouse is over the green button
			if ((x > b6.x) && (x < b6.x + b6.w) && (y > b6.y)
				&& (y < b6.y + b6.h))
			{
			  cout << "The green button has been clicked" << endl;
			  cout << "whichscreen is now 1" << endl;
			  whichscreen = 1;
			}
		  }
		}
	    //If the user has Xed out the window
	    if (event.type == SDL_QUIT)
	    {
		//Quit the program
		quit = true;
	    }
	  }
	  //$$$$$$$$ END HANDLING EVENTS OF CREDITS PAGE $$$$$$$$$
	}
  }








  return 0;
}
